﻿Imports ESRI.ArcGIS.CatalogUI
Imports ESRI.ArcGIS.Catalog
Imports System.ComponentModel

Public Class layersSiteConfigUI

    Private layersSite As BindingList(Of LayerConfig)
    Dim siteDAO As SiteDAO = New SiteDAO
    Private Sub addLayerBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addLayerBtn.Click
        Dim pGxDialog As IGxDialog
        Dim pShpFilter As IGxObjectFilter
        Dim pLyrFilter As IGxObjectFilter
        Dim pFilterCol As IGxObjectFilterCollection
        Dim pEnumGx As IEnumGxObject
        pShpFilter = New GxFilterShapefiles
        pLyrFilter = New GxFilterLayers
        pGxDialog = New GxDialog
        pFilterCol = pGxDialog
        pFilterCol.AddFilter(pShpFilter, True)
        pFilterCol.AddFilter(pLyrFilter, False) 'pLyrFilter is the default filter.
        pGxDialog.Title = "Browse Data"
        If Not pGxDialog.DoModalOpen(0, pEnumGx) Then
            Exit Sub 'Exit if user press Cancel
        End If
        Dim selectedGisFile As IGxObject = pEnumGx.Next
        If selectedGisFile Is Nothing Then
            Return
        End If
        If selectedGisFile.FullName Is Nothing Then
            Return
        End If
        Dim selectedFileFullName As String = selectedGisFile.FullName
        If isLayerExist(selectedGisFile.Name) Then
            MsgBox("La couche exist déja dans le référentiel !!", MsgBoxStyle.Exclamation, "Attention")
            Return
        End If
        Dim newConfigLayer As LayerConfig = New LayerConfig(selectedGisFile.Name, selectedGisFile.BaseName, False, "", "")
        siteDAO.addConfigLayer(newConfigLayer)
        Try
            copyLayer(selectedGisFile)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        layersSite.Add(newConfigLayer)

    End Sub
    Public Sub copyLayer(ByVal gxObject As IGxObject)
        If gxObject Is Nothing Then
            Return
        End If
        Dim defaultLayersPath As String = getWorkSpaceConfigLayerPath()
        Dim sourcePath As String = (FolderFromFileName(gxObject.FullName))
        For Each filetdx As String In IO.Directory.GetFiles(sourcePath, gxObject.BaseName & ".*")
            System.IO.File.Copy(filetdx, defaultLayersPath & "\" & NameOnlyFromFullPath(filetdx), True)
        Next
    End Sub

    Private Function isLayerExist(ByVal layerName As String) As Boolean
        For Each layer As LayerConfig In layersSite
            If layer.LayerName = layerName Then
                Return True
            End If
        Next
        Return False
    End Function

    Private Sub delLayerBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles delLayerBtn.Click
        If siteLayersDG.CurrentRow Is Nothing OrElse siteLayersDG.CurrentRow.Index < 0 Then
            Return
        End If
        Dim layerConfTodelete As LayerConfig = layersSite(siteLayersDG.CurrentRow.Index)
        If layerConfTodelete Is Nothing Then
            Return
        End If
        layersSite.Remove(layerConfTodelete)
        siteDAO.deleteConfigLayer(layerConfTodelete)
    End Sub

    Private Sub layersSiteConfigUI_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            layersSite = New BindingList(Of LayerConfig)
            siteLayersDG.DataSource = layersSite
            siteLayersDG.Columns(2).ReadOnly = False
            Dim defaultLayers As List(Of LayersSiteConfig) = siteDAO.getSiteDefaultLayers()
                If defaultLayers.Count <= 0 Then
                    Return
                End If
                For Each defLayer As LayersSiteConfig In defaultLayers
                layersSite.Add(New LayerConfig(defLayer.layerName, defLayer.layerLabel, defLayer.layerActif, defLayer.layerForm, defLayer.layerTable))
                Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        
    End Sub
End Class